<template>
	<view class="wrap" v-if="flag">
		<u-waterfall v-model="flowList">
			<template v-slot:left="{leftList}">
				<view @click="gotoHomeDetail(item.id)" class="demo-warter" v-for="(item, index) in leftList"
					:key="index">
					<!-- 警告：微信小程序中需要hx2.8.11版本才支持在template中结合其他组件，比如下方的lazy-load组件 -->
					<u-lazy-load threshold="0" border-radius="10" :image="item.imgUrl" :index="index"></u-lazy-load>
					<view class="demo-title">
						{{item.skuName}}
					</view>
					<view class="demo-tag">
						<view class="demo-tag-owner">
							包邮
						</view>


					</view>
					<!--价格 -->
					<view class="" style="display: flex;">
						<span style="color: red;font-size: 20px;font-weight: 500;">￥{{item.price}}</span>
						<view class="demo-tag-text">
							{{item.skuWant}} 人想要
						</view>
					</view>

					<view class="demo-shop" style="display: flex;justify-content: flex-start;align-items: center;">
						<img :src="item.photoUrl" alt="" style="width: 20px;height: 20px;border-radius: 20px;">
						<p style="margin-left: 5px;">{{item.nickName}} </p>
					</view>
					<!-- <u-icon name="close-circle-fill" color="#fa3534" size="34" class="u-close" @click="remove(item.id)"></u-icon> -->
				</view>
			</template>
			<template v-slot:right="{rightList}">
				<view @click="gotoHomeDetail(item.id)" class="demo-warter" v-for="(item, index) in rightList"
					:key="index">
					<u-lazy-load threshold="0" border-radius="10" :image="item.imgUrl" :index="index"></u-lazy-load>
					<view class="demo-title">
						{{item.skuName}}
					</view>
					<view class="demo-tag">
						<view class="demo-tag-owner">
							包邮
						</view>

					</view>

					<!--价格 -->
					<view class="" style="display: flex;">
						<span style="color: red;font-size: 20px;font-weight: 500;">￥{{item.price}}</span>
						<view class="demo-tag-text">
							{{item.skuWant}} 人想要
						</view>
					</view>


					<view class="demo-shop" style="display: flex;justify-content: flex-start;align-items: center;">
						<img :src="item.photoUrl" alt="" style="width: 20px;height: 20px;border-radius: 20px;">
						<p style="margin-left: 5px;">{{item.nickName}} </p>
					</view>
					<!-- <u-icon name="close-circle-fill" color="#fa3534" size="34" class="u-close" @click="remove(item.id)"></u-icon> -->
				</view>
			</template>
		</u-waterfall>
		<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="addRandomData"></u-loadmore>
	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex';

	export default {
		data() {
			return {

				flag: true,
				loadStatus: 'loadmore',
				flowList: [],
				list: null,

				// 当前选中的选项卡
				current: 0,
				// 每个选项卡初始页面
				page: 1,
				// 每次列表请求的记录条数
				limit: 5,

				pages: 0,

				total: 0,

				skuInfoVo: {}
			}
		},
		mounted() {

			//页面挂载时候需要重置page
			this.$store.state.home.page = 1
			this.getSkuPageList({
				categoryId: 1
			})

		},

		methods: {
			...mapMutations('home', [
				'setPage', // 将 `increment` mutation 映射为 `this.increment()`
				'setPages'
			]),

			async getSkuPageList(form) {

				if (form.categoryId !== null && form.categoryId !== undefined) {
					this.flag = false
					this.skuInfoVo = {
						categoryId: form.categoryId,
					}
				} else if (form.skuType !== null && form.skuType !== undefined) {
					this.flag = false
					this.skuInfoVo = {
						skuType: form.skuType,
					}
				} else if (form.keyword !== null && form.keyword !== undefined) {
					this.flag = false
					this.skuInfoVo = {
						keyword: form.keyword,
					}
				}


				const result = await this.$u.api.getskuList({
					page: this.$store.state.home.page || this.page,
					limit: this.limit,
					form: this.skuInfoVo
				});
				//分页总页数
				console.log(result);
				this.pages = result.pages
				this.setPages(
					this.pages
				)

				//如果是切换分类，则把原有数据清空，如果是触底，则合并数组
				if (form.categoryId !== null && form.categoryId !== undefined) {
					this.flowList = [...result.records];
				} else {


					//判断是否已经分页底层
					if (this.pages >= this.$store.state.home.page) {
						this.flowList = this.flowList.concat(result.records);
					} else {
						console.log("没有数据了");
					}

				}
				this.flag = true
			},

			gotoHomeDetail(skuId) {
				uni.navigateTo({
					url: '../homeDetail/homeDetail?skuId=' + skuId
				})
			},
			formatPrice(price) {
				if (!price) return '0';

				const integerPart = Math.floor(price).toString();
				return integerPart;
			},
			newFormatPrice(price) {
				if (!price) return '0.00';

				const decimalPart = (price % 1).toFixed(2).slice(2);
				return decimalPart;
			}
		},
		

	}
</script>

<style>
	/* page不能写带scope的style标签中，否则无效 */
	page {
		background-color: rgb(240, 240, 240);
	}
</style>

<style lang="scss" scoped>
	.demo-warter {
		border-radius: 8px;
		margin: 0px 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}

	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}

	.demo-image {
		width: 100%;
		border-radius: 4px;
	}

	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		/* 显示的行数 */
		-webkit-box-orient: vertical;
		overflow: hidden;


	}

	.demo-tag {
		display: flex;
		margin-top: 5px;
	}

	.demo-tag-owner {
		background-color: $u-type-primary;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}

	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}

	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}
</style>